package com.ruoyi.report_ljlm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_ljlm.domain.ReportLjlmYsdhdfjlbBaDetail;
import com.ruoyi.report_ljlm.domain.vo.ReportLjlmYsdhdfjlbBaDetailVo;
import com.ruoyi.report_ljlm.mapper.ReportLjlmYsdhdfjlbBaDetailMapper;
import com.ruoyi.report_tg.domain.TgsybgBaDetail;
import com.ruoyi.report_tg.domain.bo.TgsybgBsInfoBo;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.syrw.annotation.DealHeh;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ljlm.domain.bo.ReportLjlmYsdhdfbgBsInfoBo;
import com.ruoyi.report_ljlm.domain.vo.ReportLjlmYsdhdfbgBsInfoVo;
import com.ruoyi.report_ljlm.domain.ReportLjlmYsdhdfbgBsInfo;
import com.ruoyi.report_ljlm.mapper.ReportLjlmYsdhdfbgBsInfoMapper;
import com.ruoyi.report_ljlm.service.IReportLjlmYsdhdfbgBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 压实度试验检测报告（环刀法）Service业务层处理
 *
 * @author zyb
 * @date 2024-11-08
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class ReportLjlmYsdhdfbgBsInfoServiceImpl implements IReportLjlmYsdhdfbgBsInfoService {

    private final ReportLjlmYsdhdfbgBsInfoMapper baseMapper;
    private final ReportLjlmYsdhdfjlbBaDetailMapper detailMapper;

    /**
     * 查询压实度试验检测报告（环刀法）
     */
    @Override
    @FIllBasicInfo
    public ReportLjlmYsdhdfbgBsInfoVo queryById(Long excelId){
        ReportLjlmYsdhdfbgBsInfoVo reportLjlmYsdhdfbgBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<ReportLjlmYsdhdfjlbBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<ReportLjlmYsdhdfjlbBaDetail>()
            .eq(ReportLjlmYsdhdfjlbBaDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            reportLjlmYsdhdfbgBsInfoVo.setBaDetailVos(baDetailVos);
        }
        // 生成报告结论
        // 如果已经有值了则不需要计算结论。
        if (StrUtil.isNotEmpty(reportLjlmYsdhdfbgBsInfoVo.getJiancjl()) && !"检测结论：".equals(reportLjlmYsdhdfbgBsInfoVo.getJiancjl())) {
            return reportLjlmYsdhdfbgBsInfoVo;
        }
        return jcjl(reportLjlmYsdhdfbgBsInfoVo);
    }

    /**
     * 查询压实度试验检测报告（环刀法）列表
     */
    @Override
    public TableDataInfo<ReportLjlmYsdhdfbgBsInfoVo> queryPageList(ReportLjlmYsdhdfbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLjlmYsdhdfbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportLjlmYsdhdfbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询压实度试验检测报告（环刀法）列表
     */
    @Override
    public List<ReportLjlmYsdhdfbgBsInfoVo> queryList(ReportLjlmYsdhdfbgBsInfoBo bo) {
        LambdaQueryWrapper<ReportLjlmYsdhdfbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLjlmYsdhdfbgBsInfo> buildQueryWrapper(ReportLjlmYsdhdfbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLjlmYsdhdfbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportLjlmYsdhdfbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportLjlmYsdhdfbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), ReportLjlmYsdhdfbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportLjlmYsdhdfbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportLjlmYsdhdfbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportLjlmYsdhdfbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLjlmYsdhdfbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportLjlmYsdhdfbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportLjlmYsdhdfbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportLjlmYsdhdfbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancrq()), ReportLjlmYsdhdfbgBsInfo::getJiancrq, bo.getJiancrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportLjlmYsdhdfbgBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportLjlmYsdhdfbgBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportLjlmYsdhdfbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportLjlmYsdhdfbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportLjlmYsdhdfbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getQuywz()), ReportLjlmYsdhdfbgBsInfo::getQuywz, bo.getQuywz());
        lqw.eq(StringUtils.isNotBlank(bo.getWeitrwbh()), ReportLjlmYsdhdfbgBsInfo::getWeitrwbh, bo.getWeitrwbh());
        lqw.eq(StringUtils.isNotBlank(bo.getTuycd()), ReportLjlmYsdhdfbgBsInfo::getTuycd, bo.getTuycd());
        lqw.eq(StringUtils.isNotBlank(bo.getDaibsl()), ReportLjlmYsdhdfbgBsInfo::getDaibsl, bo.getDaibsl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportLjlmYsdhdfbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), ReportLjlmYsdhdfbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), ReportLjlmYsdhdfbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportLjlmYsdhdfbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportLjlmYsdhdfbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), ReportLjlmYsdhdfbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportLjlmYsdhdfbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportLjlmYsdhdfbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(bo.getExcelResult() != null, ReportLjlmYsdhdfbgBsInfo::getExcelResult, bo.getExcelResult());
        lqw.eq(StringUtils.isNotBlank(bo.getJianclb()), ReportLjlmYsdhdfbgBsInfo::getJianclb, bo.getJianclb());
        lqw.eq(StringUtils.isNotBlank(bo.getYasdbzz()), ReportLjlmYsdhdfbgBsInfo::getYasdbzz, bo.getYasdbzz());
        lqw.eq(StringUtils.isNotBlank(bo.getPingjz()), ReportLjlmYsdhdfbgBsInfo::getPingjz, bo.getPingjz());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancds()), ReportLjlmYsdhdfbgBsInfo::getJiancds, bo.getJiancds());
        lqw.eq(StringUtils.isNotBlank(bo.getHegs()), ReportLjlmYsdhdfbgBsInfo::getHegs, bo.getHegs());
        lqw.eq(StringUtils.isNotBlank(bo.getHegl()), ReportLjlmYsdhdfbgBsInfo::getHegl, bo.getHegl());
        return lqw;
    }

    /**
     * 新增压实度试验检测报告（环刀法）
     */
    @Override
    public Boolean insertByBo(ReportLjlmYsdhdfbgBsInfoBo bo) {
        ReportLjlmYsdhdfbgBsInfo add = BeanUtil.toBean(bo, ReportLjlmYsdhdfbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 多个keys
            ArrayList<ReportLjlmYsdhdfjlbBaDetail> details = new ArrayList<>(6);
            for (long i = 1; i <= 6; i++) {
                details.add(new ReportLjlmYsdhdfjlbBaDetail(add.getExcelId(), i));
            }
        }
        return flag;
    }

    /**
     * 修改压实度试验检测报告（环刀法）
     */
    @Override
    public Boolean updateByBo(ReportLjlmYsdhdfbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        ReportLjlmYsdhdfbgBsInfo update = BeanUtil.toBean(bo, ReportLjlmYsdhdfbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), ReportLjlmYsdhdfjlbBaDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(ReportLjlmYsdhdfbgBsInfoBo bo) {
        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
            if (bo.getHegs().equals(bo.getJiancds())) {
                bo.setExcelResult(1);
            }else {
                bo.setExcelResult(2);
            }
            return;
        }
        bo.setExcelResult(0);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLjlmYsdhdfbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除压实度试验检测报告（环刀法）
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public ReportLjlmYsdhdfbgBsInfoVo jcjl(ReportLjlmYsdhdfbgBsInfoVo vo) {
        vo.setJiancjl("压实度检测合格。");
        return vo;
    }
}
